/*
 * @Copyright: EasyCompany 2021
 * @Author: asterisk
 * @Date: 2021-03-18 19:56:45
 * @Description: 
 */
import 'package:date_time_format/date_time_format.dart';

import '../../easy_log.dart';

class EasyLogMessageFormatter implements EasyLogFormatter {
  final String formatPattern = 'Ymd-His.v';
  EasyLoggerConfig _config; //ref;
  EasyLogMessageFormatter(this._config);

  @override
  dynamic format(dynamic message) {
    return _formatTimestamp(message) +
        ' ' +
        _formatLevel(message) +
        ' ' +
        _formatTags(message) +
        ' ' +
        _formatMessage(message);
  }

  String _formatTimestamp(EasyLogMessage message) {
    final dtl = DateTime.now().toLocal();
    return '[' + dtl.format(formatPattern) + ']';
  }

  String _formatTags(EasyLogMessage message) {
    var appName = message.tags['appName'];
    var logName = message.tags['logName'];
    var className = message.tags['className'];
    var funcName = message.tags['functionName'];
    return (null != logName ? '[' + logName + ']' : '') +
        (null != className ? '[' + className + ']' : '') +
        (null != funcName ? '[' + funcName + ']' : '');
  }

  String _formatLevel(EasyLogMessage message) {
    var placeholder = '';
    switch (message.level.value) {
      case 0:
        placeholder = ' ' * 1;
        break;
      case 1:
        break;
      case 2:
        placeholder = ' ' * 2;
        break;
      case 3:
        placeholder = ' ' * 3;
        break;
      case 4:
        break;
      case 5:
        placeholder = ' ' * 2;
        break;
      case 6:
        placeholder = ' ' * 2;
        break;
      case 7:
        placeholder = ' ' * 2;
        break;
      case 8:
        placeholder = ' ' * 6;
        break;
      case 10:
        placeholder = ' ' * 1;
        break;
      default:
        break;
    }
    return '[' + message.level.name + ']' + placeholder;
  }

  String _formatError(EasyLogMessage message) {
    return '';
  }

  String _formatStackTrace(EasyLogMessage message) {
    return '';
  }

  String _formatMessage(EasyLogMessage message) {
    if (message.message is String) {
      return message.message;
    } else {
      return message.message.toString();
    }
  }
}
